package whu.o2o.adapter;

import java.util.ArrayList;

import whu.o2o.DiaryBaseActivity;
import whu.o2o.GuideActivity;
import whu.o2o.GuideCardActivity;
import whu.o2o.MyApplication;
import whu.o2o.View.ExpandGridView;
import whu.o2o.View.ExpandListView;
import whu.o2o.util.ConstantData;
import whu.o2o.util.Utils;
import whu.o2o.vo.CommentVO;
import whu.o2o.vo.DiaryVO;
import whu.o2o.vo.ImageVO;
import whu.o2o.vo.PersonVO;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import whu.o2o.R;




public class DiaryAdapter extends BaseAdapter implements OnClickListener {
	private MyApplication mApplication;
	private Activity mContext;
	private LayoutInflater mLayoutInflater;
	private ArrayList<DiaryVO> mList;
	private Drawable mValidatedDrawable;
	private Drawable mManDrawable;
	private Drawable mWoManDrawable;
	private int mImagesTwoColumnWidth;
	private int mImagesThreeColumnWidth;
	private int mDiaryType;

	private Handler mHandler = new Handler(new Handler.Callback() {

		@Override
		public boolean handleMessage(Message msg) {
			return false;
		}

	});

	public DiaryAdapter(Activity context, ArrayList<DiaryVO> list, int diaryType) {
		mContext = context;
		mList = list;
		mLayoutInflater = LayoutInflater.from(mContext);
		mApplication = (MyApplication) mContext.getApplication();
		mDiaryType = diaryType;
		init();
	}

	private void init() {
		mValidatedDrawable = mContext.getResources().getDrawable(R.drawable.attestation_v);
		mValidatedDrawable.setBounds(0, 0, mValidatedDrawable.getMinimumWidth(), mValidatedDrawable.getMinimumHeight());

		mManDrawable = mContext.getResources().getDrawable(R.drawable.boy);
		mManDrawable.setBounds(0, 0, mManDrawable.getMinimumWidth(), mManDrawable.getMinimumHeight());

		mWoManDrawable = mContext.getResources().getDrawable(R.drawable.girl);
		mWoManDrawable.setBounds(0, 0, mWoManDrawable.getMinimumWidth(), mWoManDrawable.getMinimumHeight());

		mImagesTwoColumnWidth = Utils.dip2px(mContext, 143);
		mImagesThreeColumnWidth = Utils.dip2px(mContext, 216);
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final int index = position;
		DiaryVO diaryVO = mList.get(index);

		ViewHolder holder;
		if (convertView == null) {
			convertView = mLayoutInflater.inflate(R.layout.listitem_diary, null);

			holder = new ViewHolder();
			holder.userHead = (ImageView) convertView.findViewById(R.id.user_head);
			holder.userName = (TextView) convertView.findViewById(R.id.user_name);
			//holder.userAge = (TextView) convertView.findViewById(R.id.user_age);
			holder.content = (TextView) convertView.findViewById(R.id.content);
			holder.time = (TextView) convertView.findViewById(R.id.time);
			holder.state = (TextView) convertView.findViewById(R.id.state);
			holder.retry = (TextView) convertView.findViewById(R.id.retry);
			holder.singleImage = (ImageView) convertView.findViewById(R.id.single_image);
			holder.images = (ExpandGridView) convertView.findViewById(R.id.images);
			holder.delete = (TextView) convertView.findViewById(R.id.delete);
			//holder.good = (TextView) convertView.findViewById(R.id.good);
			//holder.comment = (TextView) convertView.findViewById(R.id.comment);
			//holder.commentList = (ExpandListView) convertView.findViewById(R.id.comment_listView);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		setUserInfo(holder, diaryVO);
		setSendState(holder, diaryVO);
		setDeleteState(holder.delete, diaryVO);

		if (!TextUtils.isEmpty(diaryVO.getContent())) {
			holder.content.setText(diaryVO.getContent());
		} else {
			holder.content.setText("");
		}

		if (!TextUtils.isEmpty(diaryVO.getTime())) {
			holder.time.setText(diaryVO.getTime());
		} else {
			holder.time.setText("");
		}

		setImages(holder, diaryVO);
		//setGoodState(holder.good);
		//setCommentList(holder.commentList, diaryVO);

		holder.userHead.setOnClickListener(this);

		//holder.comment.setTag(index);
		//holder.comment.setOnClickListener(this);

		return convertView;
	}

	@Override
	public void onClick(View view) {

		switch (view.getId()) {
		case R.id.user_head:
			Intent intent = new Intent(mContext, GuideCardActivity.class);
			mContext.startActivity(intent);
			break;

		case R.id.comment:
			if (view.getTag() != null && view.getTag() instanceof Integer) {
				int index = (Integer) view.getTag();
				CommentVO commentVO = new CommentVO();
				commentVO.setIndex(index);
				commentVO.setHandler(mHandler);
				commentVO.setDiaryId(mList.get(index).getId());

				if (mContext instanceof DiaryBaseActivity) {
					((DiaryBaseActivity) mContext).showCommentBox(commentVO);
				}

			}
			break;
		}

	}

	private void setUserInfo(ViewHolder holder, DiaryVO diaryVO) {
		PersonVO personVO = diaryVO.getUser();
		if (personVO != null) {
			if (!TextUtils.isEmpty(personVO.getName())) {
				holder.userName.setText(personVO.getName());
			} else {
				holder.userName.setText("");
			}

			if (personVO.isValidated()) {
				holder.userName.setCompoundDrawables(null, null, mValidatedDrawable, null);
			} else {
				holder.userName.setCompoundDrawables(null, null, null, null);
			}

			//holder.userAge.setText(String.format(mContext.getResources().getString(R.string.age), personVO.getAge()));

			if (personVO.getSex() == ConstantData.Data.WOMAN) {
				//holder.userAge.setCompoundDrawables(mWoManDrawable, null, null, null);
			} else {
				//holder.userAge.setCompoundDrawables(mManDrawable, null, null, null);
			}

		} else {
			holder.userName.setText("");
			holder.userName.setCompoundDrawables(null, null, null, null);
			//holder.userAge.setText(String.format(mContext.getResources().getString(R.string.age), 0));
			//holder.userAge.setCompoundDrawables(mManDrawable, null, null, null);
		}

	}

	/**
	 * 发送状态
	 */
	private void setSendState(ViewHolder holder, DiaryVO diaryVO) {
		switch (diaryVO.getSendState()) {

		// 已发送
		case ConstantData.Data.SENDED_DIARY_STATE:
			holder.time.setText(diaryVO.getTime()); // 显示发送时间
			holder.time.setVisibility(View.VISIBLE);
			holder.state.setVisibility(View.GONE);
			holder.retry.setVisibility(View.GONE);
			break;

		// 正在发送
		case ConstantData.Data.SENDING_DIARY_STATE:
			holder.state.setVisibility(View.VISIBLE); // 显示发送中
			holder.retry.setVisibility(View.GONE);
			holder.time.setVisibility(View.GONE);
			break;

		// 发送失败
		case ConstantData.Data.FAIL_DIARY_STATE:
			holder.retry.setVisibility(View.VISIBLE); // 显示重试”
			holder.state.setVisibility(View.GONE);
			holder.time.setVisibility(View.GONE);
			break;
		}
	}

	/**
	 * 删除
	 */
	private void setDeleteState(TextView delete, DiaryVO diaryVO) {
		PersonVO personVO = diaryVO.getUser();
		if (personVO != null && personVO.getId() == mApplication.getUserId()
				&& diaryVO.getSendState() != ConstantData.Data.SENDING_DIARY_STATE) {
			delete.setVisibility(View.VISIBLE);
		} else {
			delete.setVisibility(View.GONE);
		}
	}

	/**
	 * 显示图片
	 */
	private void setImages(ViewHolder holder, DiaryVO diaryVO) {
		if (diaryVO.getImages() != null && diaryVO.getImages().size() > 0) {
			if (diaryVO.getImages().size() == 1) {
				// 显示单图
				holder.singleImage.setVisibility(View.VISIBLE);
				holder.images.setVisibility(View.GONE);

				ImageVO imageVO = diaryVO.getImages().get(0);

			} else {
				// 显示多图
				holder.singleImage.setVisibility(View.GONE);
				holder.images.setVisibility(View.VISIBLE);

				LayoutParams params = holder.images.getLayoutParams();
				if (diaryVO.getImages().size() == 4) {
					holder.images.setNumColumns(2);
					params.width = mImagesTwoColumnWidth;
				} else {
					params.width = mImagesThreeColumnWidth;
					holder.images.setNumColumns(3);
				}
				initGridView(holder.images, diaryVO.getImages());
			}

		} else {
			// 没有图片
			holder.singleImage.setVisibility(View.GONE);
			holder.images.setVisibility(View.GONE);
		}

	}

	/**
	 * 显示多张图片
	 */
	private void initGridView(ExpandGridView gridView, ArrayList<ImageVO> imageList) {
		DiaryImageAdapter adapter = (DiaryImageAdapter) gridView.getAdapter();
		if (adapter == null) {
			adapter = new DiaryImageAdapter(mContext, imageList);
			gridView.setAdapter(adapter);
		} else {
			adapter.publicList = imageList;
			adapter.notifyDataSetChanged();
		}

	}

	private void setGoodState(TextView good) {
		if (mDiaryType == ConstantData.Data.FAQ_DIARY_TYPE) {
			good.setVisibility(View.GONE);
		} else {
			good.setVisibility(View.VISIBLE);
		}
	}

	/**
	 * 显示评论列表
	 */
	private void setCommentList(ExpandListView commentList, DiaryVO diaryVO) {
		if (diaryVO.getComments() != null && diaryVO.getComments().size() > 0) {
			commentList.setVisibility(View.VISIBLE);
			initCommentList(commentList, diaryVO.getComments());
		} else {
			commentList.setVisibility(View.GONE);
		}

	}

	private void initCommentList(ExpandListView listView, ArrayList<CommentVO> commentList) {
		DiaryCommentAdapter adapter = (DiaryCommentAdapter) listView.getAdapter();
		if (adapter == null) {
			adapter = new DiaryCommentAdapter(mContext, commentList);
			listView.setAdapter(adapter);
		} else {
			adapter.publicList = commentList;
			adapter.notifyDataSetChanged();
		}
	}

	static class ViewHolder {
		ImageView userHead;
		TextView userName;
		TextView userAge;
		TextView content;
		TextView time;
		TextView state;
		TextView retry;
		ImageView singleImage;
		ExpandGridView images;
		TextView delete;
		TextView good;
		TextView comment;
		ExpandListView commentList;
	}

}
